package com.globagames.strategicgames.command
{
	
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.globagames.strategicgames.business.*;
	import com.globagames.strategicgames.event.*;
	import com.globagames.strategicgames.model.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	

	public class LoginRegistrationCommand implements Command, IResponder
	{
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		
		private var delegate : PlayerDelegate;
		private var sessionDelegate : SessionDelegate;
		private var connexionEvent:ConnexionEvent;
		public function LoginRegistrationCommand()
		{		
		}
	
		public function execute( event : CairngormEvent ): void
		{	
			connexionEvent = event as ConnexionEvent;
			if(connexionEvent.action=='register'){			
				delegate = new PlayerDelegate( this );	   
				delegate.doCreate(connexionEvent.player);	
			}
			if(connexionEvent.action=='login'){							
				delegate = new PlayerDelegate( this );	   
				delegate.doFind(connexionEvent.player);
				
			}				
			if(connexionEvent.action=='disconnect'){
				delegate = new PlayerDelegate( this );	   
				delegate.doDisconnectPlayer(connexionEvent.player);									
			}
			if(connexionEvent.action=='fill-sessions'){
				delegate = new PlayerDelegate( this );	   
				delegate.doFindConnectedPlayers(connexionEvent.player);
				
			}	
			if(connexionEvent.action=='session-commit'){
				sessionDelegate = new SessionDelegate( this );
				sessionDelegate.doCommit();
				
			}
			if(connexionEvent.action=='touch'){
				delegate = new PlayerDelegate( this );	   
				delegate.doTouch(connexionEvent.player);	
				
			}	
					
			
		}
		
		public function result( event : Object ) : void
		{	
			if(connexionEvent.action=='login' || connexionEvent.action=='register'){	
				var player : Player = Player( event.result );
				if( player ) {	
					if( player.state == "LOGGED" ) {
					trace("[LoginRegistrationCommand] player \"" + player.name + "\" with playerId \""+player.playerId +"\" logged");
					model.player = player;
					connexionEvent.isLogged = true;
					} else {
						connexionEvent.errorMessage = "ALREADY_LOGGED";
						connexionEvent.hasError = true;	
					}
				} else {
					connexionEvent.errorMessage = "NOT_FOUND";
					connexionEvent.hasError = true;					
				}
			}
			if(connexionEvent.action=='connect'){
				var session : Session = Session( event.result );	
				model.session = session;
				connexionEvent.isConnected = true;
			}
			if(connexionEvent.action=='fill-sessions'){
				
				var sessions : ArrayCollection = ArrayCollection( event.result );
				
				model.players = sessions;
				connexionEvent.isFilledWithSessions = true;
			}
			if(connexionEvent.action=='disconnect'){
				sessionDelegate = new SessionDelegate( this );
				sessionDelegate.doDisconnect();
			}
			
			
						
		}
		
		public function fault( event : Object ) : void
		{
			Alert.show(event.toString());
			trace(event.toString());//, event.fault.faultCode);
		}
		
		
		
	}

}